﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp;
using DevExpress.Persistent.Base.General;
using System.ComponentModel;

namespace DomainComponents.Common {
    [DomainComponent]
    public interface IDCTreeNode : ITreeNode {
        new String Name { get; set; }
        IDCTreeNode Owner { get; set; }
        IList<IDCTreeNode> Nodes { get; }
    }
    [DomainLogic(typeof(IDCTreeNode))]
    public class IDCTreeNodeLogic {
        private ProxyCollection children;
        //private void objectSpace_Reloaded(object sender, EventArgs e) {
        //    ((IObjectSpace)sender).Reloaded -= new EventHandler(objectSpace_Reloaded);
        //    if(children != null) {
        //        children.Dispose();
        //        children = null;
        //    }
        //}
        public ITreeNode Get_Parent(IDCTreeNode instance) {
            return instance.Owner;
        }
        public IBindingList Get_Children(IDCTreeNode instance, IObjectSpace objectSpace) {
            if(children == null) {
                //objectSpace.Reloaded += new EventHandler(objectSpace_Reloaded);
                ITypeInfo typeInfo = XafTypesInfo.Instance.FindTypeInfo(typeof(IDCTreeNode));
                children = new ProxyCollection(objectSpace, typeInfo, instance.Nodes);
            }
            return children;
        }
    }
}
